#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
int main()
{
    char *dirName = "test_dir";
    // 创建目录
    if (mkdir(dirName, 0755) != 0)
    {
        perror("mkdir() failed");
        return -1;
    }
    printf("mkdir success\n");

    // 创建文件
    FILE *files[3] = {NULL};
    char filename[50];
    for (int i = 0; i < 3; i++)
    {
        snprintf(filename, sizeof(filename),
                 "./%s/test_file%d.txt", dirName, i + 1);
        files[i] = fopen(filename, "w+");
        if (files[i] == NULL)
        {
            perror("fopen failed");
            return -1;
        }
    }
    printf("create file success\n");

    // 关闭文件
    for (int i = 0; i < 3; i++)
    {
        fclose(files[i]);
    }

    // 更改目录权限
    if (chmod(dirName, 0700) != 0)
    {
        perror("chmod() failed");
        return -1;
    }
    printf("chmod success\n");

    // 使用stat函数获取目录信息
    struct stat dirStat;
    if (stat(dirName, &dirStat) != 0)
    {
        printf("stat() failed");
        return -1;
    }
    printf("size : %ld bytes\n", (long)dirStat.st_size);
    printf("mode: %lo (octal)\n", (unsigned long)dirStat.st_mode & 0777);

    // 获取目录中的文件信息
    DIR *dir;
    struct dirent *entry;
    dir = opendir(dirName);
    if (dir == NULL)
    {
        perror("opendir() failed");
        return -1;
    }
    printf("directory files:\n");
    while ((entry = readdir(dir)) != NULL)
    {
        printf("%s\n", entry->d_name);
    }
    closedir(dir);
    return 0;
}